package com.ptp.initializer;

import com.ptp.handler.WebSocketHandler;
import io.netty.channel.Channel;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.group.ChannelGroup;
import io.netty.handler.codec.http.HttpObjectAggregator;
import io.netty.handler.codec.http.HttpServerCodec;
import io.netty.handler.codec.http.websocketx.WebSocketServerProtocolHandler;
import io.netty.handler.stream.ChunkedWriteHandler;

/**
 * 
 * @author nnli
 *
 */
public class WebSocketInitializer extends ChannelInitializer<Channel>{

	private final ChannelGroup channelGroup;
	
	public WebSocketInitializer(ChannelGroup channelGroup){
		this.channelGroup = channelGroup;
	}
	
	@Override
	protected void initChannel(Channel ch) throws Exception {
		ChannelPipeline pipeline = ch.pipeline();
		pipeline.addLast(new HttpServerCodec());
		pipeline.addLast(new ChunkedWriteHandler());
		pipeline.addLast(new HttpObjectAggregator(64 * 1024));
		pipeline.addLast(new WebSocketHandler("/websocket"));
		pipeline.addLast(new WebSocketServerProtocolHandler("/websocket"));
//		pipeline.addLast(new TextWebSocketFrameHandler(channelGroup));
	}

}
